// #pragma option -WS

#include <WinTen.h>
#include <Windows.h>
#include <ClipApi.h>

extern HANDLE __hInstance;

static PCLIPVAR CurrentLBase;
static LPCLIPSYMBOL pHandleEvent = 0;


#define hWND   1    //<<<<< ---------   OJO con no cambiar el orden
                    //                      de declaraciones de Instvars
                    //                      en la Clase TWindow.

//----------------------------------------------------------------------------//

BOOL far pascal _export DialogWndProc( HWND hDlg, WORD wMsg, WORD wParam,
                                   LONG lParam )
{
   if( ! pHandleEvent )
       pHandleEvent = _Get_Sym( "HandleEvent" );

   _PutQ( hDlg );
   _cAtPut( CurrentLBase + 5, hWND, _tos-- );

   _PutSym( pHandleEvent );
   _bcopy( ( void * ) ++_tos, ( void * ) ( CurrentLBase + 5 ), 14 );
   _PutQ( wMsg );
   _PutQ( wParam );
   _PutLN( lParam );
   _xSend( 3 );

    return (BOOL) ( _eval->wType ); // _parl( - 1 );
}

//----------------------------------------------------------------------------//

CLIPPER DIALOGBOX()
{
   PCLIPVAR OldLBase   = CurrentLBase;
   FARPROC  DlgWndProc = MakeProcInstance( ( FARPROC ) DialogWndProc,
                                           __hInstance );
   WORD wResult;

   CurrentLBase = _lbase;
   wResult = DialogBox( ( HANDLE ) _parni( 1 ), ( LPSTR ) _parc( 2 ),
                     ( HWND ) _parni( 3 ), DlgWndProc );
   CurrentLBase = OldLBase;
   FreeProcInstance( DlgWndProc );
   _retni( wResult );
}

//----------------------------------------------------------------------------//

CLIPPER DIALOGBOXI()                // DialogBoxIndirect
{
   PCLIPVAR OldLBase = CurrentLBase;
   HGLOBAL hMem;
   BYTE * pDlgTemp, * pString;
   WORD wResult, wLen;
   HCURSOR crsOld;
   FARPROC _DlgBoxInd = MakeProcInstance( ( FARPROC ) DialogWndProc,
                                           __hInstance );
   if( ! _parclen( 2 ) )
   {
      _retni( 0 );
      return;
   }

   crsOld = SetCursor( LoadCursor( 0, IDC_WAIT ) );
   pString  = _parc( 2 );
   wLen     = _parclen( 2 );
   hMem     = GlobalAlloc( GHND, wLen );
   pDlgTemp = ( BYTE * ) GlobalLock( hMem );

   for( wResult = 0; wResult < wLen; wResult++ )
      * ( pDlgTemp + wResult ) = * ( pString + wResult );

   GlobalUnlock( ( HGLOBAL ) pDlgTemp );

   CurrentLBase = _lbase;
   SetCursor( crsOld );
   wResult      = DialogBoxIndirect( _parni( 1 ), hMem, _parni( 3 ),
                                     _DlgBoxInd );
   CurrentLBase = OldLBase;
   FreeProcInstance( _DlgBoxInd );
   GlobalFree( hMem );
   _retni( wResult );
}

//----------------------------------------------------------------------------//

CLIPPER ENDDIALOG()
{
   EndDialog( ( HWND ) _parni( 1 ), ( WORD ) _parni( 2 ) );
}

//----------------------------------------------------------------------------//
